const host = 'https://ysts.yax.com.cn';

/**
 * 请求数据接口GET
 */
function get(url,data){
  return new Promise((resolve,reject)=>{
    let header = {"content-type": "application/json"};
    getHc('token').then(res=>{
      console.log("--当前token--",res)
      //判断是否有token
      if(res!=''){
        header = Object.assign(header,{
          "Authorization":`Bearer ${res}`
        })
      }
      // console.log(header)
      wx.request({
        method:'get',
        url: `${host}${url}`,
        data: data,
        header: header,
        success: (res) =>{
          if(res.data.code == 20000 || res.data.code == 10081|| res.data.code == 10080){
            login(function(){post(url,data).then(res=>{resolve(res)})})
          }else{
            if(res.statusCode == 500){
              wx.hideLoading()
            }else{
              resolve(res)
            }
          }
        },
        fail: (err) => {
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                title: '请求失败',
                icon:'none'
              })
            },
          })
          reject(err)
        }
      })
    })
    
  })
}
/**
 * 请求数据接口POST
 */
function post(url,data,func){
  return new Promise((resolve,reject)=>{
    let header = {"content-type": "application/x-www-form-urlencoded"};
    getHc('token').then(res=>{
      console.log("--当前token--POST",url,"token为",res)
      //判断是否有token
      if(res!=''){
        header = Object.assign(header,{
          "Authorization":`Bearer ${res}`
        })
      }
      // console.log(header)
      // 发起请求
      wx.request({
        method:'post',
        url: `${host}${url}`,
        data: data,
        header: header,
        success: (res) =>{
          if(res.data.code == 20000 || res.data.code == 10081|| res.data.code == 10080){
            console.log("请求未登录")
            login(function(){
              console.log("现在进入重新请求数据中");
                post(url,data).then(res=>{
                  resolve(res)
                })
            })
          }else{
            if(res.statusCode == 500){
              wx.hideLoading();
            }else{
              resolve(res)
            }
          }
        },
        fail: (err) => {
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                title: '请求失败',
                icon:'none'
              })
            },
          })
          reject(err)
        }
      })
    })
    
  })
}

/**
 * 获取缓存数据
 */
function getHc(name){
  return new Promise((resolve,reject)=>{
    wx.getStorage({
      key: name,
      success:function(res){
        resolve(res.data)
      },
      fail:function(res){
        resolve('')
      }
    })
  })
}

/**
 * 重新获取token
 * data: 新用户绑定推荐人
 */
function login(func,data){
  //微信获取code
  wx.login({
    success: (wxres)=>{
      // console.log(wxres.code);
      let obj = {code:wxres.code};
      if(data) obj = Object.assign(obj,data);
      //获取新的token
      get('/api/auth/get_token',obj).then(res=>{
        console.log("后台获取token",res)
        // 缓存sessionkey
        wx.setStorage({
          data: res.data.data.session_key,
          key: 'session_key',
        })
        // 缓存token
        wx.setStorage({
          data: res.data.data.token,
          key: 'token',
        })
        func(res);
      })
    }
  })
}

/**
 * 电话咨询
 */
function phone(){
  wx.makePhoneCall({
    phoneNumber: '073185580888',
    success(){console.log("发起电话咨询")},
    fail(){console.log("取消电话咨询")},
    complete(){console.log("电话咨询完成")}
  })
}

/**
 * 接口暴露
 */
module.exports ={
  get,
  post,
  host, //请求头
  getHc, //获取缓存
  login, //重新登录获取token
  phone, //电话咨询 
}